package heima3_2.cn.osc.presenter;

import java.util.ArrayList;
import java.util.List;

import heima3_2.cn.osc.model.bean.DynamicFragmentBean.Tweet;
import heima3_2.cn.osc.model.bean.DynamicFragmentBean.TweetsList;
import heima3_2.cn.osc.ui.main_fragment.DynamicFragment.DynamicFragment;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by 超能浣熊 on 2017/1/15.
 */

public class DynamicPresenter extends BasePresenter<TweetsList> {
    DynamicFragment dynamicFragment;
    private int id;
    public List<Tweet> mTweetList = new ArrayList<>();
    private int mFlag;

    public DynamicPresenter(DynamicFragment d, int uid) {
        super();
        this.dynamicFragment = d;
        this.id = uid;
    }


    @Override
    protected void getFailureData(Throwable t) {
        t.printStackTrace();
//        dynamicFragment.listViewsDyn.setIsLoading(false);
    }

    @Override
    protected void getSuccessData(Response<TweetsList> response) {
        TweetsList body = response.body();
        if (mFlag == 0) {
            mTweetList.removeAll(mTweetList);
            mTweetList = body.getTweetslist();
        } else {

            mTweetList.addAll(response.body().getTweetslist());

            if (response.body().getTweetslist().size() < 20) {
                dynamicFragment.listViewsDyn.setHasMoreItems(false);
            }
        }

        if (mTweetList.size() < 20) {
            dynamicFragment.listViewsDyn.setHasMoreItems(false);
        }

        dynamicFragment.dynamicAdapter.setDatas(mTweetList);

        if (dynamicFragment.listViewsDyn != null) {
            dynamicFragment.listViewsDyn.setIsLoading(false);
        }

//        dynamicFragment.listViewsDyn.addFooterView();
    }

//    @Override
//    public void getData(int page) {
//        mFlag = page;
//        switch (page) {
//            case 0:
//                Call<TweetsList> info = mRetrofitInterface.getTweetInfo(0, 0, 20);
//                info.enqueue(mCallback);
//                break;
//            case 1:
//                Call<TweetsList> info1 = mRetrofitInterface.getTweetInfo(-1, 0, 20);
//                info1.enqueue(mCallback);
//                break;
//            case 2:
//                Call<TweetsList> info2 = mRetrofitInterface.getTweetInfo(993896, 0, 20);
//                info2.enqueue(mCallback);
//                break;
//            default:
//                Call<TweetsList> info3 = mRetrofitInterface.getTweetInfo(page, 0, 20);
//                info3.enqueue(mCallback);
//                break;
//        }
//
//    }

    @Override
    public void getData(int page) {
        mFlag = page;
        switch (id) {
            case 0:
                Call<TweetsList> info = mRetrofitInterface.getTweetInfo(0, page, 20);
                info.enqueue(mCallback);
                break;
            case 1:
                Call<TweetsList> info1 = mRetrofitInterface.getTweetInfo(-1, page, 20);
                info1.enqueue(mCallback);
                break;
            default:
                Call<TweetsList> info2 = mRetrofitInterface.getTweetInfo(993896, page, 20);
                info2.enqueue(mCallback);
                break;

        }
//        if (id == 0) {
//            Call<TweetsList> info = mRetrofitInterface.getTweetInfo(0, 0, 20);
//            info.enqueue(mCallback);
//        } else if (id == 1) {
//            Call<TweetsList> info = mRetrofitInterface.getTweetInfo(-1, 0, 20);
//            info.enqueue(mCallback);
//        } else {
//            Call<TweetsList> info = mRetrofitInterface.getTweetInfo(993896, 0, 20);
//            info.enqueue(mCallback);
//        }

    }
}
